/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) 
    {
        ListNode* first = new ListNode(NULL);
        first -> next = head;                    //添加头结点
        ListNode* ptr = first;                   //搜索节点
        ListNode* current = first ;           //中间节点
        
        for (int i = 0; i < n; i++)
        {
            current = current -> next;
        }
        
        while (current -> next != NULL)  //到达最后一个节点
        {
            ptr = ptr -> next;
            current = current -> next;
        }
        
        ListNode* p = ptr -> next;
        ptr -> next = p -> next;
        
        delete p;
        
        return first -> next;     
    }
};